package com.onpositive.datamap.values.impl;

import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.data.units.Unit;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.IDimensionRange;
import com.onpositive.datamap.core.IDataProperty;
import com.onpositive.datamap.core.IDimensionProperty;
import com.onpositive.datamap.core.impl.UnitFormatter;

public class DimensionRange extends ScalarRange implements IDimensionRange{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DimensionRange(IDataProperty prop, Object comment, double low,
			double high) {
		super(prop, comment, low, high);
	}

	public DimensionRange(IDataProperty prop, Object comment, double low) {
		super(prop, comment, low);
	}

	@Override
	public Unit getUnit() {
		return ((IDimensionProperty)prop).getPrimaryUnit();
	}

	@Override
	public String toString() {
		if (isNone()){
			return "None";
		}
		if (isVarious()){
			return "Various";
		}
		if (lowValue==highValue){
			return UnitFormatter.format(lowValue, getUnit().getShortName());
		}
		String format = UnitFormatter.format(lowValue, getUnit().getShortName());
		String format2 = UnitFormatter.format(highValue, getUnit().getShortName());
		int lastIndexOf = format.lastIndexOf(' ');
		if (lastIndexOf!=-1){
			String un=format.substring(lastIndexOf+1);
			int lastIndexOf1 = format2.lastIndexOf(' ');
			if (lastIndexOf1!=-1){
				String un1=format2.substring(lastIndexOf1+1);
				if (un.equals(un1)){
					return format.substring(0,lastIndexOf)+"-"+format2;
				}
			}
		}
		return format+"-"+format2;
	}
	@Override
	public IDataPropertyValue clone(AbstractValueAccessor accessor) {
		return new DimensionRange(prop.clone(accessor),getComment(),lowValue,highValue);
	}
}
